package com.tl.khadmin.controller;
 
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.tl.khadmin.bean.OpenOrder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.tl.khadmin.bean.Order;
import com.tl.khadmin.bean.OrderException; 
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.OrderIService;
import com.tl.khadmin.util.CommonUtil;


@RequestMapping(value = "/order")
@Controller
public class OrderController {
	
	@Resource
	private OrderIService orderService;
	
	@RequestMapping(value = "/showorderbypage")
	@ResponseBody
	public Map<String, Page<Order>> showOrderByPage(HttpSession session, @RequestParam Map<String, Object> params) {
		Integer pageNum = Integer.parseInt(params.get("pageNum").toString());
		Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
		return CommonUtil.returnResultCodeJson("result", orderService.showOrderByPage(session, pageNum, pageSize, params));
	} 
	
	@RequestMapping(value = "/showorderinfo")
	@ResponseBody
	public Map<String, Object> showOrderInfo(HttpSession session, Integer id) {
		if(id == null){
			return null;
		}
		return orderService.showOrderInfo(id);
	} 
	
	@RequestMapping(value = "/accept")
	@ResponseBody
	public String acceptOrder(String orderIds) {
		if(StringUtils.isBlank(orderIds)){
			return CommonUtil.returnMapResultByRows(0, "","参数错误");
		} 
		return orderService.acceptOrder(orderIds);
	}
	
	/**
	 * 上传订单异常证明图片
	 * @param session
	 * @param file
	 * @return
	 */
	@RequestMapping(value="/uploadabnormalimg")  
	@ResponseBody
	public Map<String, String> upLoadDeclarationImg(HttpSession session, @RequestParam("file") MultipartFile file){
		String path = session.getServletContext().getRealPath("/upload/productimg");
		return orderService.uploadAbnormalImg(path,file); 
	}
	
	/**
	 * 保存订单异常信息
	 * @param orderException
	 * @return
	 */
	@RequestMapping(value = "/abnormal/save")
	@ResponseBody
	public String saveOrderException(HttpSession session, OrderException orderException){
		return orderService.saveOrderException(session, orderException);
	} 
	
	/**
	 * 订单发货
	 * @param orderException
	 * @return
	 */
	@RequestMapping(value = "/logistics/save")
	@ResponseBody
	public String saveOrderLogistics(HttpSession session, Order order){
		return orderService.saveOrderLogistics(session, order);
	}

	@RequestMapping(value = "/saveOrder")
	@ResponseBody
	public String saveOrder(@RequestBody String p)  {
		OpenOrder order = JSON.parseObject(p,OpenOrder.class);

		return orderService.saveOrder(order);



	}

}
